<?php

/**
 * This is the model class for table "block".
 *
 * The followings are the available columns in table 'block':
 * @property int $id
 * @property string $name
 * @property int $title_id
 * @property int $text_id
 */
class Block extends AActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Block the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'block';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, name, title_id, text_id', 'required'),
			array('id, title_id, text_id', 'length', 'max'=>10),
			array('name', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, title_id, text_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'title' => array(self::BELONGS_TO, 'Text', 'title_id'),
			'text' => array(self::BELONGS_TO, 'Text', 'text_id'),
			'blockCategoryConnects'=>array(self::HAS_MANY, 'BlockCategoryConnect', 'block_id'),
			'imageGroup' => array(self::BELONGS_TO, 'ImageGroup',  'image_group_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Название',
			'title_id' => 'Заголовок',
			'title.string' => 'Заголовок',
			'text_id' => 'Текст',
			'text.string' => 'Текст',
			'category'=>'Категория',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('title_id',$this->title_id,true);
		$criteria->compare('text_id',$this->text_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	protected function getDeleteConfig(){
		return array(
			'title',
			'text',
			'blockCategoryConnects',
			'imageGroup',
		);
	}
}